﻿using System.IO;
using System.Reflection;
using Magellan.ProjectWizards.Manager.Helpers;
using Microsoft.Win32;

namespace Magellan.ProjectWizards.Manager.Commands
{
    public class InstallTemplateCommand : ICommand
    {
        public bool Execute()
        {
            if (!VisualStudioHelper.EnsureVisualStudioIsNotRunning())
            {
                return false;
            }

            var common = VisualStudioHelper.GetCommon7Path();
            var windowsTemplates = Path.Combine(common, @"IDE\ProjectTemplates\CSharp\Windows");

            var sourcePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "VisualStudio\\Magellan.zip");
            foreach (var locale in Directory.GetDirectories(windowsTemplates))
            {
                File.Copy(sourcePath, Path.Combine(locale, "Magellan.zip"), true);
            }

            Registry.LocalMachine.SetValue("SOFTWARE\\Magellan\\Dependencies", Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
            VisualStudioHelper.Setup();
            return true;
        }
    }
}